import 'package:flutter/material.dart';
import 'package:flutterdemo/dart/function_page.dart';
import 'package:flutterdemo/library/bloc/counter_page.dart';
import 'package:flutterdemo/pages/battery_page.dart';
import 'package:flutterdemo/pages/bluetooth_client_page.dart';
import 'package:flutterdemo/pages/chart_page.dart';
import 'package:flutterdemo/pages/city_page.dart';
import 'package:flutterdemo/pages/event_bus_page.dart';
import 'package:flutterdemo/pages/face_compare_page.dart';
import 'package:flutterdemo/pages/http_page.dart';
import 'package:flutterdemo/pages/lottery_page.dart';
import 'package:flutterdemo/pages/map_page.dart';
import 'package:flutterdemo/pages/mqtt_page.dart';
import 'package:flutterdemo/pages/notification_page.dart';
import 'package:flutterdemo/pages/package_page.dart';
import 'package:flutterdemo/pages/scan_page.dart';
import 'package:flutterdemo/pages/screen_page.dart';
import 'package:flutterdemo/pages/sqlite_page.dart';
import 'package:flutterdemo/pages/swiper_page.dart';
import 'package:flutterdemo/pages/url_launcher_page.dart';
import 'package:flutterdemo/pages/video_page.dart';
import 'package:flutterdemo/pages/videoplayer_page.dart';
import 'package:flutterdemo/utils/toast_util.dart';
import 'package:flutterdemo/widgets/button.dart';
import 'package:flutterdemo/widgets/check.dart';
import 'package:flutterdemo/widgets/cupertino/button_cur.dart';
import 'package:flutterdemo/widgets/custom.dart';
import 'package:flutterdemo/widgets/dialog.dart';
import 'package:flutterdemo/widgets/gridview.dart';
import 'package:flutterdemo/widgets/icon.dart';
import 'package:flutterdemo/widgets/input.dart';
import 'package:flutterdemo/widgets/layout.dart';
import 'package:flutterdemo/widgets/progress.dart';
import 'package:flutterdemo/widgets/statusbar_page.dart';
import 'package:flutterdemo/widgets/tabbar.dart';
import 'package:permission_handler/permission_handler.dart';

import 'inherited_page.dart';

class ListViewWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ListViewState();
  }
}

class ListViewState extends State<ListViewWidget> {
  List<RouteModel> routeModels = [];
  DateTime _lastDateTime;

  @override
  void initState() {
    super.initState();
    _initPermission();
    routeModels.add(RouteModel("图表", ChartPage()));
    routeModels.add(RouteModel("人脸对比", FaceComparePage()));
    routeModels.add(RouteModel("蓝牙", BluetoothClientPage()));
    routeModels.add(RouteModel("bloc模式", CounterPage()));
    routeModels.add(RouteModel("notification", NotificationPage()));
    routeModels.add(RouteModel("mqtt", MqttPage()));
    routeModels.add(RouteModel("InheritedWidget", InheritedPage()));
    routeModels.add(RouteModel("video 播放", VideoPage()));
    routeModels.add(RouteModel("video player", VideoPlayerPage()));
    routeModels.add(RouteModel("swiper", SwiperPage()));
    routeModels.add(RouteModel("函数式编程", FunctionPage()));
    routeModels.add(RouteModel("screen适配", ScreenPage()));
    routeModels.add(RouteModel("statusbar", StatusbarPage()));
//    routeModels.add(RouteModel("生物识别", BiometricPage()));
    routeModels.add(RouteModel("应用、设备信息", PackagePage()));
//    routeModels.add(RouteModel("camera", CameraPage()));
    routeModels.add(RouteModel("插件", BatterPage()));
    routeModels.add(RouteModel("dio网络请求", HttpPage()));
    routeModels.add(RouteModel("lottery", LotteryPage()));
    routeModels.add(RouteModel("event_bus", EventBusPage()));
    routeModels.add(RouteModel("url_launcher", UrlLauncherPage()));
    routeModels.add(RouteModel("GridView", GridViewPage()));
    routeModels.add(RouteModel("二维码扫描", ScanPage()));
    routeModels.add(RouteModel("sqflite缓存", SqlitePage()));
    routeModels.add(RouteModel("百度地图", BaiduMapPage()));
    routeModels.add(RouteModel("城市列表", CitySelectPage("title")));
    routeModels.add(RouteModel("button", ButtonWidget()));
    routeModels.add(RouteModel("ios button", ButtonCupWidget()));
    routeModels.add(RouteModel("icon、image", IconWidget()));
    routeModels.add(RouteModel("单选、复选", CheckWidget()));
    routeModels.add(RouteModel("输入框和表单", TextFieldWidget()));
    routeModels.add(RouteModel("progress", ProgressWidget()));
    routeModels.add(RouteModel("layout", LayoutWidget()));
    routeModels.add(RouteModel("TabBar", TabBarWidget()));
    routeModels.add(RouteModel("dialog", DialogWidget()));
    routeModels.add(RouteModel("自定义组件", CustomRoute()));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "ListView",
          textAlign: TextAlign.end,
        ),
      ),
      body: WillPopScope(
        onWillPop: () async {
          if (_lastDateTime == null ||
              DateTime.now().difference(_lastDateTime) > Duration(seconds: 1)) {
            _lastDateTime = DateTime.now();
            ToastUtil.showToastCenter("连续点击将退出app");
            return Future.value(false);
          } else {
            print("退出应用");
            return Future.value(true);
          }
        },
        child: ListView.builder(
          itemCount: routeModels.length * 2,
//          itemExtent: 30.0,
          itemBuilder: (BuildContext context, int position) {
            if (position.isOdd) {
              // 在每一列之前，添加一个1像素高的分隔线widget
              return Divider();
            }
            // 语法 "i ~/ 2" 表示i除以2，但返回值是整形（向下取整），比如i为：1, 2, 3, 4, 5
            // 时，结果为0, 1, 1, 2, 2， 这可以计算出ListView中减去分隔线后的实际单词对数量
            int index = position ~/ 2;
            return getItem(index);
          },
        ),
      ),
    );
  }

  Widget getItem(int position) {
    return ListTile(
      leading: Icon(Icons.comment),
      title: Container(
        height: 50,
        alignment: Alignment.centerLeft,
        child: Text(
          routeModels[position].name,
          textAlign: TextAlign.left,
          style: TextStyle(
            fontSize: 18,
            color: Colors.black54,
            height: 1.2,
            background: Paint()..color = Colors.yellowAccent,
          ),
        ),
      ),
      selected: false,
      onTap: () {
        print("onTap $position");
        Navigator.push(context, MaterialPageRoute(builder: (context) {
          return routeModels[position].routeWidget;
        }));
      },
      onLongPress: () {
        ToastUtil.showToastCenter("onLongPress $position");
      },
    );
  }

  void _initPermission() async {
    Map<Permission, PermissionStatus> map = await [
      Permission.storage,
      Permission.location,
      Permission.camera,
      Permission.storage,
      Permission.phone,
      Permission.speech,
      Permission.notification,
    ].request();
  }
}

///ListView实体
class RouteModel {
  String name;
  Widget routeWidget;

  RouteModel(this.name, this.routeWidget);
}
